package com.opeace

import org.springframework.dao.DataIntegrityViolationException

class PersonFormController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [personFormInstanceList: PersonForm.list(params), personFormInstanceTotal: PersonForm.count()]
    }

    def create() {
        [personFormInstance: new PersonForm(params)]
    }

    def save() {
//        params.information = params.information.encodeAsHTML()
//        String information = new String(params.information)
//        information = information.replace("\n", "</br>")
//        params.information = information
        def personFormInstance = new PersonForm(params)
        if (!personFormInstance.save(flush: true)) {
            render(view: "create", model: [personFormInstance: personFormInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'personForm.label', default: 'PersonForm'), personFormInstance.id])
        redirect(action: "show", id: personFormInstance.id)
    }

    def show() {
        def personFormInstance = PersonForm.get(params.id)
        String information = new String(personFormInstance.information);
        information = information.replace("\n", "</br>");
//        contents = contents.encodeAsHTML()
        personFormInstance.information = information
        if (!personFormInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'personForm.label', default: 'PersonForm'), params.id])
            redirect(action: "list")
            return
        }

        [personFormInstance: personFormInstance]
    }

    def edit() {
        def personFormInstance = PersonForm.get(params.id)
        String information = new String(personFormInstance.information);
        information = information.replace("</br>", "\n");
        personFormInstance.information = information
        if (!personFormInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'personForm.label', default: 'PersonForm'), params.id])
            redirect(action: "list")
            return
        }

        [personFormInstance: personFormInstance]
    }

    def update() {
        def personFormInstance = PersonForm.get(params.id)
        if (!personFormInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'personForm.label', default: 'PersonForm'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (personFormInstance.version > version) {
                personFormInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'personForm.label', default: 'PersonForm')] as Object[],
                          "Another user has updated this PersonForm while you were editing")
                render(view: "edit", model: [personFormInstance: personFormInstance])
                return
            }
        }


//        params.information = params.information.encodeAsHTML()
//        String information = new String(params.information)
//        information = information.replace("\n", "</br>")
//        params.information = information
        personFormInstance.properties = params
        if (!personFormInstance.save(flush: true)) {
            render(view: "edit", model: [personFormInstance: personFormInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'personForm.label', default: 'PersonForm'), personFormInstance.id])
        redirect(action: "show", id: personFormInstance.id)
    }

    def delete() {
        def personFormInstance = PersonForm.get(params.id)
        if (!personFormInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'personForm.label', default: 'PersonForm'), params.id])
            redirect(action: "list")
            return
        }

        try {
            personFormInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'personForm.label', default: 'PersonForm'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'personForm.label', default: 'PersonForm'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
